# 基于 Ubuntu 22.04
FROM ubuntu:22.04

# 替换容器内 Ubuntu 源为清华源
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list \
    && sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

# 设置时区
ENV TZ='Asia/Shanghai'

# 安装必要工具
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install --yes \
        tzdata \
        ca-certificates \
        apt-mirror \
        cron \
    && DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --assume-yes --auto-remove \
    && DEBIAN_FRONTEND=noninteractive apt-get clean --assume-yes \
    && rm -rf /var/lib/apt/lists/* \
    && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime

# 创建脚本
RUN echo > /usr/local/sbin/start.sh \
    && chmod 500 /usr/local/sbin/start.sh \
    && echo 'sed s/UBUNTU_SOURCE/${UBUNTU_SOURCE}/g /etc/apt/apt-mirror.sample | tee /etc/apt/apt-mirror.list' >> /usr/local/sbin/start.sh \
    && echo 'crontab /etc/cron.d/apt-mirror' >> /usr/local/sbin/start.sh \
    && echo 'cron -f' >> /usr/local/sbin/start.sh

# 创建日志目录（用于存储同步日志）
RUN mkdir -p /var/spool/apt-mirror/var \
    && chmod 755 /var/spool/apt-mirror

# 设置入口命令（执行脚本并保持前台运行）
CMD ["bash", "-c", "/usr/local/sbin/start.sh"]